package com.example.wan1.adapter;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextUtils;
import android.util.TypedValue;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.wan1.R;
import com.example.wan1.constant.Constants;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.utils.GlideRoundedCornersTransform;
import com.example.wan1.utils.SPUtils;
import com.example.wan1.utils.ScreenUtils;
import com.example.wan1.widget.ZQImageViewRoundOval;

import java.util.List;

import androidx.annotation.Nullable;

/***
 * 创建时间：2020/7/1 20:43
 * 创建人：10850
 * 功能描述：
 */
public class ProjectListAdapter extends BaseQuickAdapter<ArticleBean, BaseViewHolder> {
    private String mType;//最新项目还是项目列表，控制分类的显示

    public ProjectListAdapter(int layoutResId, @Nullable List<ArticleBean> data, String type) {
        super(layoutResId, data);
        mType = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, ArticleBean item) {
        //增加圆角
        ZQImageViewRoundOval iv = helper.getView(R.id.project_list_iv);
        iv.setType(ZQImageViewRoundOval.TYPE_ROUND);
        //iv.setRoundRadius(16);

        //https://blog.csdn.net/MingJieZuo/article/details/83788735
        //使用主题颜色，当作placeholder
        TypedValue typedValue = new TypedValue();
        mContext.getTheme().resolveAttribute(R.attr.colorPrimary,
                typedValue, true);
        int data = typedValue.data;
        ColorDrawable drawable1 = new ColorDrawable(data);

        String theme_color = (String) SPUtils.get(mContext, "theme_color", "");
        if (!TextUtils.isEmpty(theme_color)) {
            drawable1.setColor(Color.parseColor(theme_color));
        }

        RequestOptions myOptions = new RequestOptions().optionalTransform
                (new GlideRoundedCornersTransform(ScreenUtils.dip2px(mContext, 5f)
                        , GlideRoundedCornersTransform.CornerType.ALL))/*.placeholder(R.drawable.image_place_holder)*/
                .placeholder(drawable1);
        Glide.with(mContext).load(item.getEnvelopePic()).apply(myOptions).into(iv);


        helper.setText(R.id.project_list_author, item.getAuthor())
                .setText(R.id.project_list_title, item.getTitle())
                .setText(R.id.project_list_desc, Html.fromHtml(item.getDesc()).toString())
                .setText(R.id.project_list_time, item.getNiceDate());

        if (item.isCollect())
            helper.setImageResource(R.id.project_list_collect, R.mipmap.heart_red);
        else
            helper.setImageResource(R.id.project_list_collect, R.mipmap.heart_gray);


        if (Constants.PROJECT_NEWEST_TYPE.equals(mType))
            helper.setGone(R.id.project_list_chaptername, true).setText(R.id.project_list_chaptername, item.getChapterName());
        else
            helper.setGone(R.id.project_list_chaptername, false);

        Drawable drawable = mContext.getResources().getDrawable(R.mipmap.user);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        TextView view = helper.getView(R.id.project_list_author);
        view.setCompoundDrawables(drawable, null, null, null);

        helper.addOnClickListener(R.id.project_list_chaptername)
                .addOnClickListener(R.id.project_list_collect);
    }
}
